package cn.gene.store.order.webapi.service.impl;

import cn.gene.store.order.service.IOmsOrderService;
import cn.gene.store.order.webapi.mapper.OmsOrderMapper;
import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.common.restful.ResponseCode;
import cn.gene.timeyan.pojo.store.order.dto.ExchangeOrderDTO;
import cn.gene.timeyan.pojo.store.order.dto.OrderAddDTO;
import cn.gene.timeyan.pojo.store.order.model.OmsOrder;
import cn.gene.timeyan.pojo.store.order.vo.OrderAddVO;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 订单功能*
 *
 * @author LWG
 * @date 2022/7/23 13:02
 * @since 1.0.0@gene.cn
 */
@DubboService
@Service
@Slf4j
public class OmsOrderServiceImpl implements IOmsOrderService {
    @Autowired
    OmsOrderMapper omsOrderMapper;
    @Override
    public OrderAddVO addOrder(OrderAddDTO orderAddDTO) {
        //积分判断
        Integer moneyValue = omsOrderMapper.selectByUserId(orderAddDTO.getUserId());
        Integer money = orderAddDTO.getActualScore();
        if(moneyValue==null){
            throw new TimeyanServiceException(ResponseCode.ERR_SCORE_NULL,"积分不存在");
        }
        if(moneyValue<money){
            throw new TimeyanServiceException(ResponseCode.ERR_MONEY_LESS,"积分不足");
        }
        //库存判断
        Integer stockValue = omsOrderMapper.selectByGoodsId(orderAddDTO.getGoodsId());
        Integer stock = orderAddDTO.getPurchaseVolume();
        if(stockValue==null){
            throw new TimeyanServiceException(ResponseCode.ERR_SCORE_NULL,"商品不存在");
        }
        if(stockValue<stock){
            throw new TimeyanServiceException(ResponseCode.ERR_PRODUCT_LESS,"库存不足");
        }
        //地址判断
        if(orderAddDTO.getAddress().equals("")||orderAddDTO.getPurchaseVolume()<1){
            throw new TimeyanServiceException(ResponseCode.ERR_NOMALL,"地址或购买数量设置错误");
        }
        //下单
        OmsOrder omsOrder = new OmsOrder();
        BeanUtils.copyProperties(orderAddDTO,omsOrder);
        omsOrder.setOrderTime(LocalDateTime.now());
        int rows = omsOrderMapper.addOrder(omsOrder);
        if(rows == 0){
            throw new TimeyanServiceException(ResponseCode.ERR_VERIFICATION_CODE_VALUE,"服务器忙，下单失败");
        }
        //给用户看的订单
        OrderAddVO orderAddVO = new OrderAddVO();
        orderAddVO.setId(omsOrder.getOrderId());
        orderAddVO.setPayScore(omsOrder.getActualScore());
        orderAddVO.setCreateTime(omsOrder.getOrderTime());
        orderAddVO.setGoodsId(omsOrder.getGoodsId());
        orderAddVO.setUserId(omsOrder.getUserId());
        orderAddVO.setPurchaseVolume(omsOrder.getPurchaseVolume());
        return orderAddVO;
    }

    @Override
    public void exchange(ExchangeOrderDTO exchangeOrderDTO) {
        //积分判断
        Integer scoreValue = omsOrderMapper.selectByUserId(exchangeOrderDTO.getUserId());
        Integer score = exchangeOrderDTO.getActualScore();
        if(scoreValue==null){
            throw new TimeyanServiceException(ResponseCode.ERR_SCORE_NULL,"积分不存在");
        }
        if(scoreValue<score){
            throw new TimeyanServiceException(ResponseCode.ERR_MONEY_LESS,"积分不足");
        }
        /*减积分*/
        int scoreRows = omsOrderMapper.updateScore(exchangeOrderDTO.getUserId(),exchangeOrderDTO.getActualScore());
        if(scoreRows<1){
            throw new TimeyanServiceException(ResponseCode.ERR_VERIFICATION_CODE_VALUE,"服务器忙,稍后再试");
        }
        //库存判断
        Integer stockValue = omsOrderMapper.selectByGoodsId(exchangeOrderDTO.getGoodsId());
        Integer stock = exchangeOrderDTO.getPurchaseVolume();
        if(stockValue==null){
            //回滚，有一定缺陷
            Integer rollScore = omsOrderMapper.rollbackScore(exchangeOrderDTO.getUserId(),exchangeOrderDTO.getActualScore());
            throw new TimeyanServiceException(ResponseCode.ERR_SCORE_NULL,"商品不存在");
        }
        if(stockValue<stock){
            //回滚，有一定缺陷
            Integer rollScore = omsOrderMapper.rollbackScore(exchangeOrderDTO.getUserId(),exchangeOrderDTO.getActualScore());
            throw new TimeyanServiceException(ResponseCode.ERR_PRODUCT_LESS,"库存不足");
        }
        /*减库存*/
        Integer stockRows = omsOrderMapper.updateStock(exchangeOrderDTO.getGoodsId(),exchangeOrderDTO.getPurchaseVolume());
        if(stockRows<1){
            //回滚，有一定缺陷
            Integer rollScore = omsOrderMapper.rollbackScore(exchangeOrderDTO.getUserId(),exchangeOrderDTO.getActualScore());
            throw new TimeyanServiceException(ResponseCode.ERR_VERIFICATION_CODE_VALUE,"服务器忙稍后再试");
        }
        return ;
    }
}
